#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: link_stack.py 
@version:
@time: 2022/11/06 
"""


class StackBasedOnLinkedList:
    def __init__(self) -> None:
        self.top = None

    class Node:
        def __init__(self, data) -> None:
            self.data = data
            self.next = None

    def push(self, value):
        new_node = self.Node(value)
        if self.top is None:
            self.top = new_node
        else:
            # 因为是实现栈，新增节点 next执行前一个节点，就是之前的top
            new_node.next = self.top
            # top 赋值给new_node
            self.top = new_node

    def pop(self):
        if self.top is None:
            return -1
        # 获取栈顶结果
        result = self.top.data
        # 栈顶变为栈顶的next节点
        self.top = self.top.next
        return result


def test_static():
    stack = StackBasedOnLinkedList()
    data = [1, 2, 3, 4, 5]
    for i in data:
        stack.push(i)
    data.reverse()
    for i in data:
        assert i == stack.pop()
    assert stack.pop() == -1

if __name__ == '__main__':
    test_static()